package com.erp.erp_ui.Purchase.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.Customer.CustomerBillingPeriodRes;
import com.erp.erp_entitys.Customer.CustomerOrderAndShipInfoRes;
import com.erp.erp_entitys.Purchase.req.PurchaseOrderRestockReq;
import com.erp.erp_entitys.Purchase.req.PurchaseProductIntegrationReq;
import com.erp.erp_entitys.Purchase.req.purchaseSampleStatisticsReq;
import com.erp.erp_entitys.Purchase.res.PurchaseOrderRestockRes;
import com.erp.erp_entitys.Purchase.res.PurchaseProductIntegrationRes;
import com.erp.erp_entitys.Purchase.res.purchaseSampleStatisticsRes;
import com.erp.erp_servers.Purchase.IPurchaseService;
import com.erp.utils.DateUtils;
import com.erp.utils.StringUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-10-25 22:09
 */
@Service
public class PurchaseAllService {
    /**
     * 采购主信息接口
     */
    private final IPurchaseService purchaseService;

    public PurchaseAllService(IPurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }



    /**
     * 获取String类型不为空数据
     *
     * @param value 目标数据
     * @return 不为空数据
     */
    private String getNotNullOfStringInfo(String value) {
        if (StringUtils.isEmpty(value)) {
            return "";
        } else {
            return value;
        }
    }
    /**
     * 获取BigDecimal类型不为空的数据
     *
     * @param value 目标数据
     * @return 不为空数据
     */
    private BigDecimal getNotNullOfBigDecimalInfo(BigDecimal value) {
        if (StringUtil.isNotEmpty(value)) {
            return value;
        } else {
            return new BigDecimal("0");
        }
    }

    /**
     * 补货采购订单--集合
     * @param req
     * @return
     */
    private List<PurchaseOrderRestockRes> purchaseOrderRestockList(PurchaseOrderRestockReq req){
        List<PurchaseOrderRestockRes> list=purchaseService.purchaseOrderRestockList(req);
        for(PurchaseOrderRestockRes p:list){
            p.setExportSigningDateStr(DateUtils.format(p.getExportSigningDate(),DateUtils.DATE_PATTERN));
            p.setPurchaseSigningDateStr(DateUtils.format(p.getPurchaseSigningDate(),DateUtils.DATE_PATTERN));
        }
        return list;
    }
    /**
     * 补货采购订单--分页
     * @param req
     * @return
     */
    public PageInfo<PurchaseOrderRestockRes> purchaseOrderRestockByPage(PurchaseOrderRestockReq req){
        List<PurchaseOrderRestockRes> list=purchaseOrderRestockList(req);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    /**
     * 补货采购订单--导出
     * @param req
     * @return
     */
    public JSONObject purchaseOrderRestockExport(PurchaseOrderRestockReq req){
        List<PurchaseOrderRestockRes> list=purchaseOrderRestockList(req);
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitlePurchaseOrderRestockInfo();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if (CollectionUtils.isEmpty(list)) {
            obj.put("data", data);
            return obj;
        }
        excelPurchaseOrderRestockInfoExport(data, list);
        obj.put("data", data);
        return obj;
    }
    private JSONArray getTitlePurchaseOrderRestockInfo() {
        JSONArray obj = new JSONArray();
        obj.add("外销合同号");
        obj.add("CAPA号");
        obj.add("外销签约日期");
        obj.add("外销业务员");
        obj.add("业务模式");
        obj.add("客户编码");
        obj.add("客户简称");
        obj.add("采购单号");
        obj.add("采购总额");
        obj.add("采购操作员");
        obj.add("采购签约日期");
        return obj;
    }
    private void excelPurchaseOrderRestockInfoExport(JSONArray data, List<PurchaseOrderRestockRes> list) {
        for (PurchaseOrderRestockRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getExportContractNo()));
            infoList.add(getNotNullOfStringInfo(vo.getCapa()));
            infoList.add(getNotNullOfStringInfo(vo.getExportSigningDateStr()));
            infoList.add(getNotNullOfStringInfo(vo.getExportSalesmanName()));
            infoList.add(getNotNullOfStringInfo(vo.getBusinessModelName()));
            infoList.add(getNotNullOfStringInfo(vo.getCustomerCode()));
            infoList.add(getNotNullOfStringInfo(vo.getAbbreviation()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseNo()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseTotalPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseOperationName()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseSigningDateStr()));

            data.add(infoList);
        }
    }






    /**
     * 采购产品整合--集合
     * @param req
     * @return
     */
    private List<PurchaseProductIntegrationRes> purchaseProductIntegrationList(PurchaseProductIntegrationReq req){
        List<PurchaseProductIntegrationRes> list=purchaseService.purchaseProductIntegrationList(req);
        for(PurchaseProductIntegrationRes p:list){
            p.setPurchaseSigningDateStr(DateUtils.format(p.getPurchaseSigningDate(),DateUtils.DATE_PATTERN));
        }
        return list;
    }
    /**
     * 采购产品整合--分页
     * @param req
     * @return
     */
    public PageInfo<PurchaseProductIntegrationRes> purchaseProductIntegrationByPage(PurchaseProductIntegrationReq req){
        List<PurchaseProductIntegrationRes> list=purchaseProductIntegrationList(req);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    /**
     * 采购产品整合--导出
     * @param req
     * @return
     */
    public JSONObject purchaseProductIntegrationExport(PurchaseProductIntegrationReq req){
        List<PurchaseProductIntegrationRes> list=purchaseProductIntegrationList(req);
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitlePurchaseProductIntegrationInfo();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if (CollectionUtils.isEmpty(list)) {
            obj.put("data", data);
            return obj;
        }
        excelPurchaseProductIntegrationInfoExport(data, list);
        obj.put("data", data);
        return obj;
    }
    private JSONArray getTitlePurchaseProductIntegrationInfo() {
        JSONArray obj = new JSONArray();
        obj.add("产品编码");
        obj.add("配件编码");
        obj.add("规格编码");
        obj.add("中文名称");
        obj.add("采购单号");
        obj.add("外销合同号");
        obj.add("数量");
        obj.add("供应商名称");
        obj.add("采购最近签约日期");
        obj.add("采购操作员");
        obj.add("外销业务员");
        return obj;
    }
    private void excelPurchaseProductIntegrationInfoExport(JSONArray data, List<PurchaseProductIntegrationRes> list) {
        for (PurchaseProductIntegrationRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getProductCode()));
            infoList.add(getNotNullOfStringInfo(vo.getPartsCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationCode()));
            infoList.add(getNotNullOfStringInfo(vo.getProductCname()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseNo()));
            infoList.add(getNotNullOfStringInfo(vo.getExportContractNo()));
            infoList.add(vo.getNum()==null?0:vo.getNum());
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseSigningDateStr()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseOperationName()));
            infoList.add(getNotNullOfStringInfo(vo.getExportSalesmanName()));

            data.add(infoList);
        }
    }




    /**
     * 样品采购付款详情--集合
     * @param req
     * @return
     */
    private List<purchaseSampleStatisticsRes> purchaseSampleStatisticsList(purchaseSampleStatisticsReq req){
        List<purchaseSampleStatisticsRes> list=purchaseService.purchaseSampleStatisticsList(req);
        for(purchaseSampleStatisticsRes p:list){
            p.setSignTimeStr(DateUtils.format(p.getSignTime(),DateUtils.DATE_PATTERN));
            Calendar instance = Calendar.getInstance();
            instance.setTime(p.getSignTime());
            instance.add(Calendar.DAY_OF_YEAR,Integer.parseInt(p.getSupplierAccountPeriod()));
            p.setPayableDateStr(DateUtils.format(instance.getTime(),DateUtils.DATE_PATTERN));


            switch (p.getStatus()){
                case "1":
                    p.setStatus("草稿箱");
                    break;
                case "2":
                case "2.1":
                    p.setStatus("待批箱");
                    break;
                case "3":
                    p.setStatus("已批箱");
                    break;
                case "4":
                    p.setStatus("完成箱");
                    break;
                case "5":
                    p.setStatus("垃圾箱");
                    break;
            }
            switch (p.getTaxIncluded()){
                case "1":
                    p.setTaxIncluded("含税出厂价");
                    break;
                case "2":
                    p.setTaxIncluded("未税出厂价");
                    break;
                case "3":
                    p.setTaxIncluded("含税FOB价");
                    break;
                case "4":
                    p.setTaxIncluded("未税FOB价");
                    break;
                default:
                    p.setTaxIncluded("");
            }
        }
        return list;
    }
    /**
     * 样品采购付款详情--分页
     * @param req
     * @return
     */
    public PageInfo<purchaseSampleStatisticsRes> purchaseSampleStatisticsByPage(purchaseSampleStatisticsReq req){
        List<purchaseSampleStatisticsRes> list=purchaseSampleStatisticsList(req);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    /**
     * 样品采购付款详情--导出
     * @param req
     * @return
     */
    public JSONObject purchaseSampleStatisticsExport(purchaseSampleStatisticsReq req){
        List<purchaseSampleStatisticsRes> list=purchaseSampleStatisticsList(req);
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitlePurchaseSampleStatisticsInfo();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if (CollectionUtils.isEmpty(list)) {
            obj.put("data", data);
            return obj;
        }
        excelPurchaseSampleStatisticsInfoExport(data, list);
        obj.put("data", data);
        return obj;
    }
    private JSONArray getTitlePurchaseSampleStatisticsInfo() {
        JSONArray obj = new JSONArray();
        obj.add("单据类型");
        obj.add("供应商名称");
        obj.add("采购单号");
        obj.add("签约日期");
        obj.add("供应商账期");
        obj.add("应付日期");
        obj.add("操作员");
        obj.add("付款类型");
        obj.add("定金比例");
        obj.add("定金总额");
        obj.add("采购合同总额");
        obj.add("是否含税");
        obj.add("付款单号");
        obj.add("已申请金额");
        obj.add("未申请金额");
        obj.add("实付金额");
        obj.add("产品编码");
        obj.add("规格编码");
        obj.add("中文名称");
        obj.add("英文名称");
        obj.add("中文描述");
        obj.add("英文描述");
        obj.add("规格说明");
        obj.add("产品数量");
        obj.add("产品单价");
        obj.add("采购成本");
        obj.add("其它费用");
        obj.add("费用备注");
        obj.add("单据备注");
        obj.add("素材供应商");
        return obj;
    }
    private void excelPurchaseSampleStatisticsInfoExport(JSONArray data, List<purchaseSampleStatisticsRes> list) {
        for (purchaseSampleStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getStatus()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getPurchaseOrderNo()));
            infoList.add(getNotNullOfStringInfo(vo.getSignTimeStr()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierAccountPeriod()));
            infoList.add(getNotNullOfStringInfo(vo.getPayableDateStr()));
            infoList.add(getNotNullOfStringInfo(vo.getExecutorName()));
            infoList.add(getNotNullOfStringInfo(vo.getPaymentType()));
            infoList.add(getNotNullOfStringInfo(vo.getEarnestRate()));
            infoList.add(getNotNullOfStringInfo(vo.getEarnestAmount()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getTotalPurchaseAmount()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncluded()));
            infoList.add(getNotNullOfStringInfo(vo.getPaymentBillNo()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getApplicationAmount()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getUnApplicationAmount()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getTotalAmountActuallyPaid()));
            infoList.add(getNotNullOfStringInfo(vo.getProductNo()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecNo()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseName()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishName()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getNormExplain()));
            infoList.add(getNotNullOfStringInfo(vo.getAmount()));
            infoList.add(getNotNullOfStringInfo(vo.getProductUnitPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getOtherCost()));
            infoList.add(getNotNullOfStringInfo(vo.getMemoOfCost()));
            infoList.add(getNotNullOfStringInfo(vo.getMemo()));
            infoList.add(getNotNullOfStringInfo(vo.getMaterialSupplierName()));
            data.add(infoList);
        }
    }

}
